package cn.sccl.solrJ;

import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

/**
 * @author 饶爽
 * @time 2019年12月8日 下午4:31:43
 * @version 1.0.0
 * SolrJ管理
 * 增删改查
*/
public class SolrJManager {
    //添加
    @Test
    public void testAdd() throws Exception{
        
//      String baseURL = "http://localhost:8080/solr/collection1";指定工作核
        String baseURL = "http://localhost:8080/solr/";//默认为核1
        //单机版
        SolrServer solrServer = new HttpSolrServer(baseURL);
        
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "haha");
        doc.setField("name", "王小明");
        //添加
        solrServer.add(doc);
        solrServer.commit();
    }
    //删除
    @Test
    public void testDelete() throws Exception{
        
        String baseURL = "http://localhost:8080/solr/";//默认为核1
        //单机版
        SolrServer solrServer = new HttpSolrServer(baseURL);
        //全部删除
        solrServer.deleteByQuery("*:*",1000);
    }
    //更新
    @Test
    public void testUpdate() throws Exception{
        
        String baseURL = "http://localhost:8080/solr/";//默认为核1
        //单机版
        SolrServer solrServer = new HttpSolrServer(baseURL);
        //更新--实际上与添加一致,只要ID相同就是更新,ID不同就是添加
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "haha");
        doc.setField("name", "白小飞");
        
        solrServer.add(doc);
        solrServer.commit();
    }
    //查询
    @Test
    public void testSearch() throws Exception{
        
        String baseURL = "http://localhost:8080/solr/";//默认为核1
        //单机版
        SolrServer solrServer = new HttpSolrServer(baseURL);
        //查询 关键词 后挂 过滤条件 "product_catalog_name":"幽默杂货", "product_price":18.9 ...
        SolrQuery solrQuery = new SolrQuery();
        //关键词
//      solrQuery.set("q", "product_name:后挂");
        solrQuery.setQuery("product_name:后挂");
        //过滤条件 "product_catalog_name":"幽默杂货"
        solrQuery.set("fq", "product_catalog_name:幽默杂货");
        solrQuery.set("fq", "product_price:[* TO 19]");
        //价格排序
        solrQuery.addSort("product_price",ORDER.desc);
        //分页
        solrQuery.setStart(0);
        solrQuery.setRows(5);
        //默认域      
        solrQuery.set("df","product_name");
        //只查询指定域
        solrQuery.set("fl","id,product_name");
        //高亮  1.打开高亮开关 2.指定高亮域 3.添加前缀 后缀
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_name");
        solrQuery.setHighlightSimplePre("<span style='color:red'>");
        solrQuery.setHighlightSimplePost("</span>");
        //执行查询
        QueryResponse response = solrServer.query(solrQuery);
        //文档结果集
        SolrDocumentList docs = response.getResults();
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //1.Map K id V Map, 2.Map K 域名 V list, 3.List list.get(0)
        
        //总条数 
        long numFound = docs.getNumFound();
        
        System.out.println(numFound);
        System.out.println("----------");
        
        for (SolrDocument doc : docs) {
            System.out.println(doc.get("product_catalog_name"));
            System.out.println(doc.get("product_price"));
            System.out.println(doc.get("product_name"));
            System.out.println(doc.get("id"));
            System.out.println(doc.get("product_picture"));
            
            System.out.println("*************");
            Map<String, List<String>> map = highlighting.get(doc.get("id"));
            List<String> list = map.get("product_name");
            System.out.println(list.get(0));
        }
    }
}
